package controller.dictionary;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.ManagerAssessTypeServices;
import validator.NotBlankFluentValidator;
import validator.NotEmptyFluentValidator;
import wt.entity.dictionary.ManagerAssessType;
import wt.entity.dictionary.ManagerAssessTypeExample;
import wt.webPlugin.ZTree;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("dictionary/managerAssessType")
public class ManagerAssessTypeController extends BaseController {

    @Autowired
    ManagerAssessTypeServices managerAssessTypeServices;

    @RequiresPermissions("dictionary:managerAssessType:default")
    @RequestMapping("index")
    public String index() throws Exception {
        return "dictionary/managerAssessType/index";
    }

    @RequiresPermissions("dictionary:managerAssessType:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "pid", required = false) String pid, Map map) throws Exception {
        if (id != null) {
            ManagerAssessType managerAssessType;
            managerAssessType = managerAssessTypeServices.selectByPrimaryKey(id);
            if (managerAssessType == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, managerAssessType);
            map.put(StaticConstant.EDIT_URL, "dictionary/managerAssessType/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "dictionary/managerAssessType/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        map.put("pid", pid);

        return "dictionary/managerAssessType/editDlg";
    }


    @RequiresPermissions("dictionary:managerAssessType:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult add(ManagerAssessType managerAssessType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(managerAssessType.getName(), new NotBlankFluentValidator("名称"))
                .on(managerAssessType.getPid(), new NotEmptyFluentValidator("父节点"))
                .on(managerAssessType.getNum(), new NotBlankFluentValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        ManagerAssessType fatherManagerAssessType = managerAssessTypeServices.selectByPrimaryKey(managerAssessType.getPid());
//        if (0 != managerAssessType.getNum().indexOf(fatherManagerAssessType.getNum()) || managerAssessType.getNum().length() == fatherManagerAssessType.getNum().length()) {
//            commonResult.addMessage("编号不符合编码规范");
//            return commonResult.setError();
//        }
        if (fatherManagerAssessType.getCurrentDepth() >= fatherManagerAssessType.getMaxDepth()) {
            commonResult.addMessage("专业深度超出范围");
            return commonResult.setError();
        }

        managerAssessType.setCurrentDepth(fatherManagerAssessType.getCurrentDepth() + 1);
        managerAssessType.setMaxDepth(fatherManagerAssessType.getMaxDepth());
        managerAssessType.setLive(fatherManagerAssessType.getLive());
        managerAssessType.setItemId(fatherManagerAssessType.getItemId());

        managerAssessTypeServices.insertSelective(managerAssessType);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:managerAssessType:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ManagerAssessType managerAssessType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(managerAssessType.getName(), new NotBlankFluentValidator("名称"))
                .on(managerAssessType.getNum(), new NotBlankFluentValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        managerAssessType.setPid(null);

        managerAssessTypeServices.updateByPrimaryKeySelective(managerAssessType);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:managerAssessType:default")
    @ResponseBody
    @RequestMapping("updateCheck")
    public CommonResult updateCheck(@RequestParam(value = "checkNodes[]", required = false) List<String> checkNodeList, @RequestParam(value = "noCheckNodes[]", required = false) List<String> noCheckNodeList) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        managerAssessTypeServices.updateManagerAssessTypeLive(checkNodeList, noCheckNodeList);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:managerAssessType:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        managerAssessTypeServices.deleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:managerAssessType:default")
    @ResponseBody
    @RequestMapping("select")
    public JSONArray select() throws Exception {

        ManagerAssessTypeExample managerAssessTypeExample=new ManagerAssessTypeExample();
        managerAssessTypeExample.setOrderByClause("num");
        List<ManagerAssessType> managerAssessTypeList = managerAssessTypeServices.selectByExample(managerAssessTypeExample);
        JSONArray jsonArray = new JSONArray();
        for (ManagerAssessType managerAssessType : managerAssessTypeList) {
            JSONObject jsonObject;
            if (managerAssessType.getLive() == 1) {
                jsonObject = ZTree.createZTreeJSONObject(managerAssessType.getId(), managerAssessType.getPid(), managerAssessType.getNum() + " " + managerAssessType.getName(), true);
            } else {
                jsonObject = ZTree.createZTreeJSONObject(managerAssessType.getId(), managerAssessType.getPid(), managerAssessType.getNum() + " " + managerAssessType.getName());
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
